<template>
  <div class="goods-container">
    <!-- 头部导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb> -->
    <bread :params1="'商品管理'" :params2="'商品列表'"></bread>

    <el-card class="box-card">
      <div>
        <el-input
          placeholder="请输入内容"
          v-model="goods.query"
          style="width: 30%"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="goodsapi()"
          ></el-button
        ></el-input>
        <el-button type="primary" style="margin-left: 30px" @click="goodsAddBtn"
          >添加商品</el-button
        >
      </div>
      <!-- 显示的数据 -->
      <el-table
        :data="goodsList"
        border
        style="width: 100%"
        highlight-current-row
      >
        <!-- prop对应列内容的字段名，也可以使用 property 属性 -->
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间"> </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="editbtn(scope.row)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>

            <el-button
              @click="delbtn(scope.row)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>

          <!-- 确认删除弹框 -->
          <template>
            <el-button type="text" @click="delbtn"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(goods.pagenum)"
          :page-sizes="[10, 15, 20]"
          :page-size="goods.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
import { goods_api } from "@/api";
import { returnMessage } from "@/common/$message.js";

export default {
  data() {
    return {
      //  定义参数
      goods: {
        query: "",
        pagesize: 10,
        pagenum: 1
      },
      goodsList: [],
      total: 1,
      // 编辑用户的对话框   dialogFormVisible为true就显示对话框
      dialogFormVisible: false,

      formLabelWidth: "120px"
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.goods.pagesize = val;
      this.goodsapi();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.goods.pagenum = val;
      this.goodsapi();
    },
    //发送 goods_api商品列表请求数据的方法
    async goodsapi() {
      const { data: res } = await goods_api(this.goods);
      console.log(res.data, 222222222);
      this.goodsList = res.data.goods;
      //获取数据里的总页数
      this.total = res.data.total;
    },

    //删除单个用户按钮
    delbtn(rows) {
      // console.log(rows, 55555555555);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          // this.$message({
          //   type: 'success',
          //   message: '删除成功!'
          // });
          const { data: res } = await goodsDel_api(rows.id);
          // console.log(res, 999999999);
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
          } else {
            this.$message.error(res.meta.msg);
          }
          this.goodsapi();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //添加商品按钮
    goodsAddBtn() {
      this.$router.push("/goods/add");
    }
  },
  created() {
    this.goodsapi();
  }
};
</script>
<style scoped lang="less">
.goods-container {
  .box-card {
    margin-top: 16px;
  }
  .el-table {
    margin: 15px 0;
  }
}
</style>
